/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui, Huang Xiaodong, Liang Qian
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package sagroup.dracots.testing;

import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.Frame;
import java.awt.List;
import java.awt.Rectangle;
import java.awt.event.ItemEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import lights.adapters.Field;
import lights.adapters.Tuple;
import lights.interfaces.ITuple;
import lime2.LimeServerID;
import lime2.Location;
import lime2.TupleID;
import sagroup.dracots.CommandReactionListener;
import sagroup.dracots.DracoTS;
import sagroup.dracots.DracoTuple;
import sagroup.dracots.FlatShareTuple;
import sagroup.dracots.FlatSignalTuple;
import sagroup.dracots.ShareTuple;
import sagroup.dracots.SignalTuple;
import sagroup.dracots.commands.AddReplicaRequestByEntryCommand;
import sagroup.dracots.policy.RPTuple;
import sagroup.dracots.policy.ReplicationPolicy;
import sagroup.dracots.TupleType;
import sagroup.util.Util;

/**
 * A simple visual agent performing some basic operation.<br>
 * 
 * @author Liang Qian
 */

public class VisualAgent extends Frame implements Runnable {

	private DracoTS dts;

	private ITuple tuple;
	private ITuple tuple2;

	// Record the operation index.
	private int index = 0;

	// Store the added ITuples.
	private Vector<ITuple> g_tuples = new Vector<ITuple>();

	// Store servers for getAllEngagedHosts().
	private LimeServerID[] servers;
	private ArrayList<LimeServerID> arrayServers = new ArrayList();

	private Vector<LimeServerID> addedServers = new Vector<LimeServerID>();

	// Store selected servers.
	private LimeServerID[] selectedServers;

	// Store the result for all operations except "g".
	private DracoTuple result;

	// Store the results for "outg".
	private DracoTuple[] results;

	// Store tuple type for "outg".
	private Vector<TupleType> tupleTypes = new Vector<TupleType>();

	// Store tuple type.
	private TupleType tt = null;

	// Store DracoTuples for "outg(DracoTuple[])".
	private DracoTuple[] tempDracoTuple;

	// Store master ShareTuples to replicate for "rdg".
	private Vector<DracoTuple> toReplicateTuples = new Vector<DracoTuple>();

	// Store replica ShareTuples to remove for "rdg".
	private Vector<TupleID> toRemoveTuplesVID = new Vector<TupleID>();

	// Store tuples to push.
	private Vector<DracoTuple> toPushTuples = new Vector<DracoTuple>();

	// Store the results for "outgToMulti".
	private DracoTuple[][] g_results;

	// Store tupleIDs fetched from ReplicaRegistrar.
	private TupleID[] vids;

	// Store replica location.
	private Location[][] replicaLocations;

	private Boolean isRep = null;

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;

	// It's a panel for all "out" operations.
	private JPanel out_panel = null;

	private JTextField curTupleContent = null;
	private JButton addTupleContent = null;
	private JLabel jLabel1 = null;
	private JLabel tupleContent = null;
	private JButton run = null;

	// It's a panel for "change" operation.
	private JPanel change_panel = null;

	private JTextField curTupleContent4 = null;
	private JTextField curTupleContent5 = null;
	private JButton jButton = null;

	// It's a panel to list all engaged hosts.
	private JPanel engagedHosts_panel = null;

	private JLabel jLabel5 = null;

	// It's a panel for "rd" and "in" operations.
	private JPanel read_panel = null;

	private JButton jButton2 = null;

	// List the results of all operations.
	private JTextArea outPut = null;

	private JTextField curTupleContent2 = null;
	private JLabel jLabel9 = null;
	private JLabel tupleContent2 = null;
	private JButton jButton3 = null;
	private JLabel jLabel16 = null;
	private JLabel tupleContent4 = null;
	private JButton jButton4 = null;
	private JButton jButton5 = null;
	private JLabel jLabel19 = null;
	private JLabel tupleContent5 = null;

	// Choose location type: HERE or UNSPECIFIED.
	private Choice locationType = null;

	private JLabel jLabel7 = null;
	private JScrollPane jScrollPane = null;

	// It's a panel for all "g" operations.
	private JPanel outg_panel = null;

	private JTextField curTupleContent3 = null;
	private JButton jButton6 = null;
	private JButton jButton7 = null;
	private JLabel jLabel12 = null;
	private JButton jButton8 = null;
	private JTextArea tuples = null;
	private JScrollPane jScrollPane1 = null;
	private JLabel jLabel21 = null;
	private JLabel tupleContent3 = null;
	private JLabel jLabel22 = null;

	// List all engaged hosts.
	private List hosts = null;

	private JLabel startInfo = null;
	private JLabel errorInfo = null;

	private Choice dataType = null;

	private Choice dataType2 = null;

	private Choice choice = null;

	private JPanel main_panel = null;

	private Choice opType = null;

	private Choice dataType3 = null;

	private Choice dataType4 = null;

	private Choice dataType5 = null;

	private JLabel localHost = null;

	private JCheckBox monitor = null;

	private JButton jButton1 = null;

	private JPanel tupleType_panel = null;

	private JLabel jLabel3 = null;

	private JTextField tupleType = null;

	private JPanel share_signal_panel = null;

	private CheckboxGroup cbg = new CheckboxGroup();

	private CheckboxGroup cbg2 = new CheckboxGroup();

	private Checkbox share = null;

	private Checkbox signal = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel4 = null;

	private JLabel jLabel6 = null;

	private JLabel jLabel8 = null;

	private Choice choice1 = null;

	private JTextField ranTupleSize = null;

	private JLabel jLabel13 = null;

	private JButton addReplica = null;

	private Checkbox both = null;

	private Checkbox master = null;

	private Checkbox replica = null;

	private Checkbox checkbox = null;

	private JButton removeReplica = null;

	private JPanel address_panel = null;

	private JLabel jLabel11 = null;

	private JLabel jLabel14 = null;

	private JLabel jLabel15 = null;

	private JTextField hostName = null;

	private JTextField ip0 = null;

	private JTextField ip1 = null;

	private JTextField ip2 = null;

	private JTextField ip3 = null;

	private JTextField portNum = null;

	private JButton jButton10 = null;

	private JButton jButton11 = null;

	private JButton jButton9 = null;

	private JCheckBox jCheckBox = null;

	private JCheckBox matchAllTupleType = null;

	private JButton jButton12 = null;

	private JButton jButton13 = null;

	private JButton pushReplica = null;

	private JPanel clearReplica_panel = null;

	private JLabel jLabel18 = null;

	private List masterTuplesRe = null;

	private JLabel jLabel17 = null;

	private List replicaLocation = null;

	private JButton clearReplica = null;

	private JPanel RPTuple_panel = null;

	private JLabel jLabel20 = null;

	private JTextField tupleType2 = null;

	private JLabel jLabel23 = null;

	private Choice replicationPolicy = null;

	private JButton jButton14 = null;

	/**
	 * This is the default constructor
	 */
	public VisualAgent() {
		super();
		initialize();
	}

	/**
	 * Run method.
	 */
	public void run() {
		System.out.println("VisualAgent: " + Location.HERE.toString()
				+ " has been started.");
		try {
			// Here to get a DracoTupleSpace instance named testing.
			dts = DracoTS.get("testing");
			startInfo.setForeground(Color.GREEN);
			startInfo.setText("Get DracoTS Successfully.");
			System.out.println("Get DracoTS Successfully.");
			dts.setMonitorStatus(true);
			localHost.setForeground(Color.DARK_GRAY);
			localHost.setText("Local Host:  " + Location.HERE.toString());
			tuple = new Tuple();
			tuple2 = new Tuple();

		} catch (Exception e) {
			System.out.println("Operation failed!");
			startInfo.setForeground(Color.RED);
			startInfo.setText("Fail to Get DracoTs");
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		startInfo = new JLabel();
		startInfo.setFont(new Font("Tahoma", Font.PLAIN, 20));
		startInfo.setBounds(new Rectangle(421, 5, 303, 27));
		startInfo.setText("");

		jLabel22 = new JLabel();
		jLabel22.setText("Output:");
		jLabel22.setBounds(new Rectangle(18, 443, 56, 20));
		jLabel = new JLabel();
		jLabel.setText("Select:");
		jLabel.setBounds(new Rectangle(8, 55, 42, 23));
		this.setLayout(null);
		this.setSize(922, 675);
		this.setTitle("Visual Agent");
		this.add(getMain_panel(), null);
		addWindowListener(new MyWindowAdapter());
		this.setVisible(true);

		// set to enable to resize frame size or not.
		this.setResizable(false);
	}

	/*
	 * Close the visual agent frame.
	 */
	class MyWindowAdapter extends WindowAdapter {
		public void windowClosing(WindowEvent we) {
			System.out.println("\nExit DracoTS: " + Location.HERE.toString());
			dispose();
			System.exit(0);
		}
	}

	/**
	 * This method initializes out_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOut_panel() {
		if (out_panel == null) {
			jLabel13 = new JLabel();
			jLabel13.setBounds(new Rectangle(247, 154, 18, 25));
			jLabel13.setText("KB");
			tupleContent = new JLabel();
			tupleContent.setBounds(new Rectangle(32, 99, 473, 23));
			tupleContent.setText("<null>");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(17, 73, 83, 24));
			jLabel1.setText("Current Tuple:");
			out_panel = new JPanel();
			out_panel.setLayout(null);
			out_panel.setFont(new Font("Dialog", Font.PLAIN, 12));
			out_panel.setBounds(new Rectangle(17, 199, 517, 193));
			out_panel.add(getCurTupleContent(), null);
			out_panel.add(getAddTupleContent(), null);
			out_panel.add(jLabel1, null);
			out_panel.add(tupleContent, null);
			out_panel.add(getRun(), null);
			out_panel.setVisible(true);
			out_panel.add(getDataType(), null);
			out_panel.add(getRanTupleSize(), null);
			out_panel.add(jLabel13, null);
			out_panel.add(getJButton9(), null);
		}
		return out_panel;
	}

	/**
	 * This method initializes curTupleContent
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getCurTupleContent() {
		if (curTupleContent == null) {
			curTupleContent = new JTextField();
			curTupleContent.setBounds(new Rectangle(122, 42, 276, 23));
		}
		return curTupleContent;
	}

	/**
	 * This method initializes addTupleContent
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAddTupleContent() {
		if (addTupleContent == null) {
			addTupleContent = new JButton();
			addTupleContent.setBounds(new Rectangle(401, 42, 91, 23));
			addTupleContent.setText("Add Value");
			addTupleContent
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							System.out.println("actionPerformed()");
							String str = e.getActionCommand();
							if (str.equals("Add Value")) {
								if (!curTupleContent.getText().isEmpty()) {
									if (dataType.getSelectedItem().equals(
											"String"))
										tuple.add(new Field()
												.setValue(curTupleContent
														.getText()));
									else if (dataType.getSelectedItem().equals(
											"Integer"))
										tuple.add(new Field().setValue(Integer
												.parseInt(curTupleContent
														.getText())));
									else if (dataType.getSelectedItem().equals(
											"Double"))
										tuple.add(new Field().setValue(Double
												.parseDouble(curTupleContent
														.getText())));
									else if (dataType.getSelectedItem().equals(
											"Boolean"))
										tuple.add(new Field().setValue(Boolean
												.parseBoolean(curTupleContent
														.getText())));
								}

								curTupleContent.setText("");
								tupleContent.setText(tuple.toString());
							}
						}
					});
		}
		return addTupleContent;
	}

	/**
	 * This method initializes run
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRun() {
		if (run == null) {
			run = new JButton();
			run.setText("Run");
			run.setComponentOrientation(ComponentOrientation.UNKNOWN);
			run.setBounds(new Rectangle(410, 125, 97, 31));
			run.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");
					/*
					 * all types of "out"
					 */
					if (opType.getSelectedItem().equals("out(DracoTuple)")) {

						if (hosts.getItemCount() > 0) {
							servers = new LimeServerID[arrayServers.size()];
							servers = arrayServers.toArray(servers);

							int ix = hosts.getSelectedIndex();
							if (share.getState())
								result = dts.out(new FlatShareTuple(
										new TupleType(tupleType.getText()),
										new Location(servers[ix]), tuple));	
							else
								result = dts.out(new FlatSignalTuple(
										new TupleType(tupleType.getText()),
										new Location(servers[ix]), tuple));

							outPut
									.append(index + ": " + "out: " + tuple
											+ "\n");
							++index;
							outPut.append(result.toFlatTuple().toString());
							outPut.append("\n\n");
							outPut.selectAll();
						} else
							System.out.println("There're no engaged hosts.");
					} else if (opType.getSelectedItem().equals(
							"out(TupleType, ITuple, int)")) {
						if (share.getState())
							result = dts.out(new FlatShareTuple(
									new TupleType(tupleType.getText()), tuple));
						else
							result = dts.out(new FlatSignalTuple(
									new TupleType(tupleType.getText()), tuple));
						outPut.append(index + ": " + "out: " + tuple + "\n");
						++index;
						outPut.append(result.toFlatTuple().toString());
						outPut.append("\n\n");
						outPut.selectAll();
					} else if (opType.getSelectedItem().equals(
							"outShareTuple(TupleType, ITuple)")) {
						result =  dts.out(new FlatShareTuple(
								new TupleType(tupleType.getText()), tuple));
						outPut.append(index + ": " + "outShareTuple: " + tuple
								+ "\n");
						++index;
						outPut.append(result.toFlatTuple().toString());
						outPut.append("\n\n");
						outPut.selectAll();
					} else if (opType.getSelectedItem().equals(
							"outBroad(TupleType, ITuple, int)")) {
						servers = dts.getAllEngagedHosts();
						if (servers.length > 0) {
							if (share.getState())
								dts.outBroad(new FlatShareTuple(
										new TupleType(tupleType.getText()), tuple));
							else
								dts.outBroad(new FlatSignalTuple(
										new TupleType(tupleType.getText()), tuple));
							outPut.append(index + ": " + "outBroad: " + tuple
									+ "\n");
							++index;
							for (int ix = 0; ix < results.length; ++ix) {
								result = results[ix];
								outPut.append(result.toFlatTuple().toString());
								outPut.append("\n");
							}
							outPut.append("\n");
							outPut.selectAll();
						} else
							System.out.println("There're no engaged hosts.");
					} else if (opType.getSelectedItem().equals(
							"out(TupleType, Location, ITuple, int)")) {
						if (hosts.getItemCount() > 0) {
							servers = new LimeServerID[arrayServers.size()];
							servers = arrayServers.toArray(servers);

							int ix = hosts.getSelectedIndex();
							if (share.getState())
								result = dts.out(new FlatShareTuple(
										new TupleType(tupleType.getText()),
										new Location(servers[ix]), tuple));
							else
								result = dts.out(new FlatSignalTuple(
										new TupleType(tupleType.getText()),
										new Location(servers[ix]), tuple));
							outPut
									.append(index + ": " + "out: " + tuple
											+ "\n");
							++index;
							outPut.append(result.toFlatTuple().toString());
							outPut.append("\n\n");
							outPut.selectAll();
						} else
							System.out.println("There're no engaged hosts.");
					} else if (opType
							.getSelectedItem()
							.equals(
									"outToMulti(TupleType, LimeServerID[], ITuple, int)")) {
						if (hosts.getItemCount() > 0) {
							if (hosts.getSelectedIndexes().length > 0) {
								servers = new LimeServerID[arrayServers.size()];
								servers = arrayServers.toArray(servers);

								int selectedIndex[] = hosts
										.getSelectedIndexes();
								selectedServers = new LimeServerID[selectedIndex.length];
								for (int ix = 0; ix < selectedIndex.length; ++ix) {
									selectedServers[ix] = servers[selectedIndex[ix]];
								}
								if (share.getState())
									dts.outToMulti(
											new FlatShareTuple(
													new TupleType(tupleType.getText()), 
													tuple),
											Util.toLocationArray(selectedServers));
								else
									dts.outToMulti(
											new FlatSignalTuple(
													new TupleType(tupleType.getText()), 
													tuple),
											Util.toLocationArray(selectedServers));
								outPut.append(index + ": " + "outToMulti: "
										+ tuple + "\n");
								++index;
								for (int ix = 0; ix < results.length; ++ix) {
									result = results[ix];
									outPut.append(result.toFlatTuple()
											.toString());
									outPut.append("\n");
								}
								outPut.append("\n");
								outPut.selectAll();
							} else
								System.out
										.println("There're no selected hosts.");
						} else
							System.out.println("There're no engaged hosts.");
					} else if (opType.getSelectedItem().equals(
							"outSignalTuple(TupleType, Location, ITuple)")) {
						if (hosts.getItemCount() > 0) {
							servers = new LimeServerID[arrayServers.size()];
							servers = arrayServers.toArray(servers);

							int i = hosts.getSelectedIndex();
							result = dts.out(new FlatShareTuple(
									new TupleType(tupleType
									.getText()), new Location(servers[i]),
									tuple));
							outPut.append(index + ": " + "outSignalTuple: "
									+ tuple + "\n");
							++index;
							outPut.append(result.toFlatTuple().toString());
							outPut.append("\n\n");
							outPut.selectAll();
						} else
							System.out.println("There're no engaged hosts.");
					}

					tuple = new Tuple();
					tupleContent.setText("<null>");
				}
			});
		}
		return run;
	}

	/**
	 * This method initializes change_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getChange_panel() {
		if (change_panel == null) {
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(12, 138, 70, 17));
			jLabel6.setText("Add Formal:");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(364, 10, 100, 22));
			jLabel4.setText("ITuple: ");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(11, 8, 200, 24));
			jLabel2.setText("Local ShareTuple: ");
			tupleContent5 = new JLabel();
			tupleContent5.setBounds(new Rectangle(385, 114, 307, 22));
			tupleContent5.setText("<null>");
			jLabel19 = new JLabel();

			jLabel19.setBounds(new Rectangle(363, 92, 84, 20));
			jLabel19.setText("Current Tuple:");
			tupleContent4 = new JLabel();
			tupleContent4.setBounds(new Rectangle(36, 107, 310, 22));
			tupleContent4.setText("<null>");
			jLabel16 = new JLabel();
			jLabel16.setBounds(new Rectangle(12, 85, 110, 24));
			jLabel16.setText("Current Template:");
			change_panel = new JPanel();
			change_panel.setLayout(null);
			change_panel.setBounds(new Rectangle(17, 199, 698, 207));
			change_panel.add(getCurTupleContent4(), null);
			change_panel.add(getCurTupleContent5(), null);
			change_panel.add(getJButton(), null);
			change_panel.setVisible(false);
			change_panel.add(jLabel16, null);
			change_panel.add(tupleContent4, null);
			change_panel.add(getJButton4(), null);
			change_panel.add(getJButton5(), null);
			change_panel.add(jLabel19, null);
			change_panel.add(tupleContent5, null);
			change_panel.add(getDataType4(), null);
			change_panel.add(getDataType5(), null);
			change_panel.add(jLabel2, null);
			change_panel.add(jLabel4, null);
			change_panel.add(jLabel6, null);
			change_panel.add(getChoice1(), null);
		}
		return change_panel;
	}

	/**
	 * This method initializes curTupleContent4
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getCurTupleContent4() {
		if (curTupleContent4 == null) {
			curTupleContent4 = new JTextField();
			curTupleContent4.setBounds(new Rectangle(91, 36, 248, 23));
		}
		return curTupleContent4;
	}

	/**
	 * This method initializes curTupleContent5
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getCurTupleContent5() {
		if (curTupleContent5 == null) {
			curTupleContent5 = new JTextField();
			curTupleContent5.setBounds(new Rectangle(439, 37, 251, 23));
		}
		return curTupleContent5;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(294, 164, 98, 38));
			jButton.setText("Run");
			// jButton.setEnabled(true);
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					/*
					 * operation "change".
					 */
					if (matchAllTupleType.isSelected())
						tt = null;
					else
						tt = new TupleType(tupleType.getText());
					if (opType.getSelectedItem().equals(
							"change(ShareTuple, ITuple)")) {
						result = dts.update(FlatShareTuple.createTemplate(tt, tuple, 
								false), new FlatShareTuple(tuple2));

						outPut.append(index + ": " + "change " + tuple + " to "
								+ tuple2 + "\n");
						++index;
						if (result != null)
							outPut.append(result.toFlatTuple().toString());
						else
							outPut.append("null");
						outPut.append("\n\n");
						outPut.selectAll();
					}

					tuple = new Tuple();
					tuple2 = new Tuple();
					tupleContent4.setText("<null>");
					tupleContent5.setText("<null>");
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes engagedHosts_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getEngagedHosts_panel() {
		if (engagedHosts_panel == null) {
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(19, 2, 113, 22));
			jLabel5.setText("All Engaged Hosts:");
			engagedHosts_panel = new JPanel();
			engagedHosts_panel.setLayout(null);
			engagedHosts_panel.setBounds(new Rectangle(375, 85, 270, 113));
			engagedHosts_panel.add(jLabel5, null);
			engagedHosts_panel.add(getHosts(), null);
			engagedHosts_panel.setVisible(false);
		}
		return engagedHosts_panel;
	}

	/**
	 * This method initializes read_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getRead_panel() {
		if (read_panel == null) {
			jLabel8 = new JLabel();
			jLabel8.setBounds(new Rectangle(18, 84, 67, 19));
			jLabel8.setText("Add Formal:");
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(8, 119, 58, 21));
			jLabel7.setText("Location:");
			tupleContent2 = new JLabel();
			tupleContent2.setBounds(new Rectangle(28, 60, 435, 22));
			tupleContent2.setText("<null>");
			jLabel9 = new JLabel();
			jLabel9.setBounds(new Rectangle(7, 40, 110, 23));
			jLabel9.setText("Current Template:");
			read_panel = new JPanel();
			read_panel.setLayout(null);
			read_panel.setBounds(new Rectangle(17, 199, 481, 207));
			read_panel.add(getJButton2(), null);
			read_panel.setVisible(false);
			read_panel.add(getCurTupleContent2(), null);
			read_panel.add(jLabel9, null);
			read_panel.add(tupleContent2, null);
			read_panel.add(getJButton3(), null);
			read_panel.add(getLocationType(), null);
			read_panel.add(jLabel7, null);
			read_panel.add(getChoice(), null);
			read_panel.add(getDataType3(), null);
			read_panel.add(jLabel8, null);
			read_panel.add(getAddReplica(), null);
			read_panel.add(getBoth(), null);
			read_panel.add(getMaster(), null);
			read_panel.add(getReplica(), null);
			read_panel.add(getRemoveReplica(), null);
			read_panel.add(getPushReplica(), null);
		}
		return read_panel;
	}

	/**
	 * This method initializes jButton2
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(150, 151, 109, 43));
			jButton2.setText("Run");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					/*
					 * All types of "rd" and "in".
					 */
					if (matchAllTupleType.isSelected())
						tt = null;
					else
						tt = new TupleType(tupleType.getText());

					if (both.getState())
						isRep = null;
					else if (master.getState())
						isRep = false;
					else
						isRep = true;

					if (locationType.getSelectedItem().equals("Location.HERE")) {
						if (opType.getSelectedItem().equals("rd(DracoTuple)")) {
							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								result = dts.rd(template);
							}
							else if (signal.getState()) {
								SignalTuple st = new SignalTuple();
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setType(tt);
								template.setTuple(tuple);
								result = dts.rd(template);
							} else {
								DracoTuple tempDt = DracoTuple
										.createMatchAllTemplate();
								tempDt.setCurr(Location.HERE);
								result = dts.rd(tempDt);
							}

							outPut.append(index + ": " + "rd: " + tuple + "\n");
							++index;
							if (result != null)
								outPut.append(result.toFlatTuple().toString());
							else
								outPut.append("null");
							outPut.append("\n\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"rdp(DracoTuple)")) {

							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								result = dts.rdp(template);
							}
							else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setType(tt);
								template.setTuple(tuple);
								result = dts.rdp(template);
							} else {
								DracoTuple tempDt = DracoTuple
										.createMatchAllTemplate();
								tempDt.setCurr(Location.HERE);
								result = dts.rdp(tempDt);
							}

							outPut
									.append(index + ": " + "rdp: " + tuple
											+ "\n");
							++index;
							if (result != null)
								outPut.append(result.toFlatTuple().toString());
							else
								outPut.append("null");
							outPut.append("\n\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"rdg(DracoTuple)")) {

							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								results = dts.rdg(template);
							}
							else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setType(tt);
								template.setTuple(tuple);
								results = dts.rdg(template);
							} else {
								DracoTuple tempDt = DracoTuple
										.createMatchAllTemplate();
								tempDt.setCurr(Location.HERE);
								results = dts.rdg(tempDt);
							}

							outPut
									.append(index + ": " + "rdg: " + tuple
											+ "\n");
							++index;
							if (results != null) {
								boolean enableToRemoveReplica = false;
								boolean enableToPushReplica = false;
								toRemoveTuplesVID.removeAllElements();
								toPushTuples.removeAllElements();
								for (int ix = 0; ix < results.length; ++ix) {
									result = results[ix];
									outPut.append(result.toFlatTuple()
											.toString());
									outPut.append("\n");

									if (result instanceof ShareTuple
											&& !((ShareTuple) result)
													.isMaster()) {
										toRemoveTuplesVID.addElement(result
												.getVID());
										enableToRemoveReplica = true;
									}
									if (result instanceof ShareTuple
											&& ((ShareTuple) result).isMaster()) {
										toPushTuples.addElement(result);
										enableToPushReplica = true;
									}
								}
								if (enableToRemoveReplica)
									removeReplica.setVisible(true);
								else
									removeReplica.setVisible(false);

								if (enableToPushReplica) {
									pushReplica.setVisible(true);
									engagedHosts_panel.setVisible(true);
									LimeServerID[] tempServers = dts
											.getAllEngagedHosts();
									hosts.removeAll();
									for (int ix = 0; ix < tempServers.length; ++ix) {
										arrayServers.add(tempServers[ix]);
										hosts.add(tempServers[ix].toString());
									}
									if (hosts.getItemCount() > 0)
										hosts.select(0);
									hosts.setMultipleMode(true);
									address_panel.setVisible(true);
								} else {
									pushReplica.setVisible(false);
									engagedHosts_panel.setVisible(false);
									address_panel.setVisible(false);
								}
							} else {
								outPut.append("null");
								outPut.append("\n");
								removeReplica.setVisible(false);
								pushReplica.setVisible(false);
								engagedHosts_panel.setVisible(false);
								address_panel.setVisible(false);
							}

							outPut.append("\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"in(DracoTuple)")) {
							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								result = dts.in(template);
							} else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setType(tt);
								template.setTuple(tuple);
								result = dts.in(template);
							} else {
								DracoTuple tempDt = DracoTuple
										.createMatchAllTemplate();
								tempDt.setCurr(Location.HERE);
								result = dts.in(tempDt);
							}

							outPut.append(index + ": " + "in: " + tuple + "\n");
							++index;
							if (result != null)
								outPut.append(result.toFlatTuple().toString());
							else
								outPut.append("null");
							outPut.append("\n\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"inp(DracoTuple)")) {
							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								result = dts.inp(template);
							} else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setType(tt);
								template.setTuple(tuple);
								result = dts.inp(template);
							
							} else {
								DracoTuple tempDt = DracoTuple
										.createMatchAllTemplate();
								tempDt.setCurr(Location.HERE);
								result = dts.inp(tempDt);
							}

							outPut
									.append(index + ": " + "inp: " + tuple
											+ "\n");
							++index;
							if (result != null)
								outPut.append(result.toFlatTuple().toString());
							else
								outPut.append("null");
							outPut.append("\n\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"ing(DracoTuple)")) {
							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								results = dts.ing(template);
							} else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setCurr(Location.HERE);
								template.setType(tt);
								template.setTuple(tuple);
								results = dts.ing(template);
							} else {
								DracoTuple tempDt = DracoTuple
										.createMatchAllTemplate();
								tempDt.setCurr(Location.HERE);
								results = dts.ing(tempDt);
							}

							outPut
									.append(index + ": " + "ing: " + tuple
											+ "\n");
							++index;
							if (results != null) {
								for (int ix = 0; ix < results.length; ++ix) {
									result = results[ix];
									outPut.append(result.toFlatTuple()
											.toString());
									outPut.append("\n");
								}
							} else {
								outPut.append("null");
								outPut.append("\n");
							}

							outPut.append("\n");
							outPut.selectAll();
						}
					} else if (locationType.getSelectedItem().equals(
							"Location.UNSPECIFIED")) {
						if (opType.getSelectedItem().equals("rd(DracoTuple)")) {

							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								result = dts.rd(template);
							} else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setType(tt);
								template.setTuple(tuple);
								result = dts.rd(template);
							} else {
								result = dts.rd(DracoTuple
										.createMatchAllTemplate());
							}

							outPut.append(index + ": " + "rd: " + tuple + "\n");
							++index;
							if (result != null)
								outPut.append(result.toFlatTuple().toString());
							else
								outPut.append("null");
							outPut.append("\n\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"rdp(DracoTuple)")) {

							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								result = dts.rdp(template);
							} else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setType(tt);
								template.setTuple(tuple);
								result = dts.rdp(template);
							} else {
								result = dts.rdp(DracoTuple
										.createMatchAllTemplate());
							}

							outPut
									.append(index + ": " + "rdp: " + tuple
											+ "\n");
							++index;
							if (result != null)
								outPut.append(result.toFlatTuple().toString());
							else
								outPut.append("null");
							outPut.append("\n\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"rdg(DracoTuple)")) {

							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								results = dts.rdg(template);
							} else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setType(tt);
								template.setTuple(tuple);
								results = dts.rdg(template);
							} else {
								results = dts.rdg(DracoTuple
										.createMatchAllTemplate());
							}

							outPut
									.append(index + ": " + "rdg: " + tuple
											+ "\n");
							++index;
							if (results != null) {
								boolean enableToReplica = false;
								boolean enableToRemoveReplica = false;
								toReplicateTuples.removeAllElements();
								toRemoveTuplesVID.removeAllElements();
								for (int ix = 0; ix < results.length; ++ix) {
									result = results[ix];
									outPut.append(result.toFlatTuple()
											.toString());
									outPut.append("\n");

									if (result instanceof ShareTuple
											&& !result.getCurr().equals(
													Location.HERE)
											&& ((ShareTuple) result).isMaster()) {
										toReplicateTuples.addElement(result);
										enableToReplica = true;
									}
									if (result instanceof ShareTuple
											&& result.getCurr().equals(
													Location.HERE)
											&& !((ShareTuple) result)
													.isMaster()) {
										toRemoveTuplesVID.addElement(result
												.getVID());
										enableToRemoveReplica = true;
									}
								}
								if (enableToReplica)
									addReplica.setVisible(true);
								else
									addReplica.setVisible(false);

								if (enableToRemoveReplica)
									removeReplica.setVisible(true);
								else
									removeReplica.setVisible(false);
							} else {
								outPut.append("null");
								outPut.append("\n");
								addReplica.setVisible(false);
								removeReplica.setVisible(false);
							}
							outPut.append("\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"in(DracoTuple)")) {
							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								result = dts.in(template);
							} else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setType(tt);
								template.setTuple(tuple);
								result = dts.in(template);
							} else {
								result = dts.in(DracoTuple
										.createMatchAllTemplate());
							}

							outPut.append(index + ": " + "in: " + tuple + "\n");
							++index;
							if (result != null)
								outPut.append(result.toFlatTuple().toString());
							else
								outPut.append("null");
							outPut.append("\n\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"inp(DracoTuple)")) {
							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								result = dts.inp(template);
							} else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setType(tt);
								template.setTuple(tuple);
								result = dts.inp(template);
							} else {
								result = dts.inp(DracoTuple
										.createMatchAllTemplate());
							}
							outPut
									.append(index + ": " + "inp: " + tuple
											+ "\n");
							++index;
							if (result != null)
								outPut.append(result.toFlatTuple().toString());
							else
								outPut.append("null");
							outPut.append("\n\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"ing(DracoTuple)")) {
							if (share.getState()) {
								FlatShareTuple template = new FlatShareTuple().createMatchAllTemplate();
								template.setReplica(isRep);
								template.setTuple(tuple);
								template.setType(tt);
								template.setReplica(false);
							
								results = dts.ing(template);
							}else if (signal.getState()) {
								FlatSignalTuple template = new FlatSignalTuple().createMatchAllTemplate();
								template.setType(tt);
								template.setTuple(tuple);
								results = dts.ing(template);
							} else {
								results = dts.ing(DracoTuple
										.createMatchAllTemplate());
							}

							outPut
									.append(index + ": " + "ing: " + tuple
											+ "\n");
							++index;
							if (results != null) {
								for (int ix = 0; ix < results.length; ++ix) {
									result = results[ix];
									outPut.append(result.toFlatTuple()
											.toString());
									outPut.append("\n");
								}
							} else {
								outPut.append("null");
								outPut.append("\n");
							}

							outPut.append("\n");
							outPut.selectAll();
						}
					}

					tuple = new Tuple();
					tupleContent2.setText("<null>");
				}
			});
		}
		return jButton2;
	}

	/**
	 * This method initializes outPut
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getOutPut() {
		if (outPut == null) {
			outPut = new JTextArea();
			outPut.setLineWrap(true);
			outPut.setEditable(false);
		}
		return outPut;
	}

	/**
	 * This method initializes curTupleContent2
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getCurTupleContent2() {
		if (curTupleContent2 == null) {
			curTupleContent2 = new JTextField();
			curTupleContent2.setBounds(new Rectangle(107, 14, 268, 23));
		}
		return curTupleContent2;
	}

	/**
	 * This method initializes jButton3
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setBounds(new Rectangle(378, 14, 91, 23));
			jButton3.setText("Add Value");
			jButton3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");
					if (!curTupleContent2.getText().isEmpty()) {
						if (dataType3.getSelectedItem().equals("String"))
							tuple.add(new Field().setValue(curTupleContent2
									.getText()));
						else if (dataType3.getSelectedItem().equals("Integer"))
							tuple.add(new Field().setValue(Integer
									.parseInt(curTupleContent2.getText())));
						else if (dataType3.getSelectedItem().equals("Double"))
							tuple.add(new Field().setValue(Double
									.parseDouble(curTupleContent2.getText())));
						else if (dataType3.getSelectedItem().equals("Boolean"))
							tuple.add(new Field().setValue(Boolean
									.parseBoolean(curTupleContent2.getText())));
					}

					curTupleContent2.setText("");
					tupleContent2.setText(tuple.toString());
				}
			});
		}
		return jButton3;
	}

	/**
	 * This method initializes jButton4
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton();
			jButton4.setBounds(new Rectangle(246, 60, 92, 22));
			jButton4.setText("Add Value");
			jButton4.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					if (!curTupleContent4.getText().isEmpty()) {
						if (dataType4.getSelectedItem().equals("String"))
							tuple.add(new Field().setValue(curTupleContent4
									.getText()));
						else if (dataType4.getSelectedItem().equals("Integer"))
							tuple.add(new Field().setValue(Integer
									.parseInt(curTupleContent4.getText())));
						else if (dataType4.getSelectedItem().equals("Double"))
							tuple.add(new Field().setValue(Double
									.parseDouble(curTupleContent4.getText())));
						else if (dataType4.getSelectedItem().equals("Boolean"))
							tuple.add(new Field().setValue(Boolean
									.parseBoolean(curTupleContent4.getText())));
					}

					curTupleContent4.setText("");
					tupleContent4.setText(tuple.toString());
				}
			});
		}
		return jButton4;
	}

	/**
	 * This method initializes jButton5
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton5() {
		if (jButton5 == null) {
			jButton5 = new JButton();
			jButton5.setBounds(new Rectangle(593, 60, 94, 21));
			jButton5.setText("Add Value");
			jButton5.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					if (!curTupleContent5.getText().isEmpty()) {
						if (dataType5.getSelectedItem().equals("String"))
							tuple2.add(new Field().setValue(curTupleContent5
									.getText()));
						else if (dataType5.getSelectedItem().equals("Integer"))
							tuple2.add(new Field().setValue(Integer
									.parseInt(curTupleContent5.getText())));
						else if (dataType5.getSelectedItem().equals("Double"))
							tuple2.add(new Field().setValue(Double
									.parseDouble(curTupleContent5.getText())));
						else if (dataType5.getSelectedItem().equals("Boolean"))
							tuple2.add(new Field().setValue(Boolean
									.parseBoolean(curTupleContent5.getText())));
					}

					curTupleContent5.setText("");
					tupleContent5.setText(tuple2.toString());
				}
			});
		}
		return jButton5;
	}

	/**
	 * This method initializes locationType
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getLocationType() {
		if (locationType == null) {
			locationType = new Choice();
			locationType.setBounds(new Rectangle(67, 119, 139, 31));
			locationType.add("Location.HERE");
			locationType.add("Location.UNSPECIFIED");
			locationType.select("Location.HERE");
		}
		return locationType;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane
					.setHorizontalScrollBarPolicy(jScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jScrollPane.setBounds(new Rectangle(17, 463, 524, 166));
			jScrollPane.setViewportView(getOutPut());
			jScrollPane
					.setVerticalScrollBarPolicy(jScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		}
		return jScrollPane;
	}

	/**
	 * This method initializes outg_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOutg_panel() {
		if (outg_panel == null) {
			errorInfo = new JLabel();
			errorInfo.setFont(new Font("SansSerif", Font.PLAIN, 16));
			errorInfo.setForeground(Color.red);
			errorInfo.setBounds(new Rectangle(104, 182, 114, 22));
			errorInfo.setText("");
			tupleContent3 = new JLabel();
			tupleContent3.setBounds(new Rectangle(21, 87, 327, 21));
			tupleContent3.setText("<null>");
			jLabel21 = new JLabel();
			jLabel21.setBounds(new Rectangle(2, 67, 83, 18));
			jLabel21.setText("Current Tuple:");
			jLabel12 = new JLabel();
			jLabel12.setBounds(new Rectangle(355, 6, 47, 23));
			jLabel12.setText("Tuples:");
			outg_panel = new JPanel();
			outg_panel.setLayout(null);
			outg_panel.setBounds(new Rectangle(17, 199, 628, 217));
			outg_panel.add(getCurTupleContent3(), null);
			outg_panel.add(getJButton6(), null);
			outg_panel.add(getJButton7(), null);
			outg_panel.add(jLabel12, null);
			outg_panel.add(getJButton8(), null);
			outg_panel.add(getJScrollPane1(), null);
			outg_panel.add(jLabel21, null);
			outg_panel.add(tupleContent3, null);
			outg_panel.setVisible(false);
			outg_panel.add(errorInfo, null);
			outg_panel.add(getDataType2(), null);
		}
		return outg_panel;
	}

	/**
	 * This method initializes curTupleContent3
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getCurTupleContent3() {
		if (curTupleContent3 == null) {
			curTupleContent3 = new JTextField();
			curTupleContent3.setBounds(new Rectangle(82, 31, 262, 23));
		}
		return curTupleContent3;
	}

	/**
	 * This method initializes jButton6
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton6() {
		if (jButton6 == null) {
			jButton6 = new JButton();
			jButton6.setBounds(new Rectangle(247, 55, 96, 21));
			jButton6.setText("Add Value");
			jButton6.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					if (!curTupleContent3.getText().isEmpty()) {
						if (dataType2.getSelectedItem().equals("String"))
							tuple.add(new Field().setValue(curTupleContent3
									.getText()));
						else if (dataType2.getSelectedItem().equals("Integer"))
							tuple.add(new Field().setValue(Integer
									.parseInt(curTupleContent3.getText())));
						else if (dataType2.getSelectedItem().equals("Double"))
							tuple.add(new Field().setValue(Double
									.parseDouble(curTupleContent3.getText())));
						else if (dataType2.getSelectedItem().equals("Boolean"))
							tuple.add(new Field().setValue(Boolean
									.parseBoolean(curTupleContent3.getText())));
					}

					curTupleContent3.setText("");
					tupleContent3.setText(tuple.toString());
				}
			});
		}
		return jButton6;
	}

	/**
	 * This method initializes jButton7
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton7() {
		if (jButton7 == null) {
			jButton7 = new JButton();
			jButton7.setBounds(new Rectangle(268, 110, 71, 24));
			jButton7.setText("OK");
			jButton7.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					errorInfo.setText("");
					g_tuples.addElement(tuple);
					tupleTypes.addElement(new TupleType(tupleType.getText()));
					tuples.append(tuple.toString());
					tuple = new Tuple();
					tuples.append("\n");
					tuples.selectAll();
					tupleContent3.setText("<null>");
				}
			});
		}
		return jButton7;
	}

	/**
	 * This method initializes jButton8
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton8() {
		if (jButton8 == null) {
			jButton8 = new JButton();
			jButton8.setBounds(new Rectangle(222, 166, 113, 41));
			jButton8.setText("Run");
			jButton8.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					if (g_tuples.isEmpty())
						errorInfo.setText("No tuples. -_-");

					if (!g_tuples.isEmpty()) {
						/*
						 * All types of "outg".
						 */
						if (opType.getSelectedItem().equals(
								"outg(DracoTuple[])")) {
							if (hosts.getItemCount() > 0) {

								servers = new LimeServerID[arrayServers.size()];
								servers = arrayServers.toArray(servers);
								int inx = hosts.getSelectedIndex();

								tempDracoTuple = new DracoTuple[g_tuples.size()];
								if (share.getState())
									for (int ix = 0; ix < g_tuples.size(); ++ix)
										tempDracoTuple[ix] = new FlatShareTuple(
												tupleTypes.elementAt(ix),
												new Location(servers[inx]),
												g_tuples.elementAt(ix));
								else
									for (int ix = 0; ix < g_tuples.size(); ++ix)
										tempDracoTuple[ix] = new FlatSignalTuple(
												tupleTypes.elementAt(ix),
												new Location(servers[inx]),
												g_tuples.elementAt(ix));
														
														

								results = dts.outg(tempDracoTuple);
								outPut.append(index + ": " + "outg: "
										+ g_tuples + "\n");
								++index;
								for (int ix = 0; ix < results.length; ++ix) {
									result = results[ix];
									outPut.append(result.toFlatTuple()
											.toString());
									outPut.append("\n");
								}
								outPut.append("\n");
								outPut.selectAll();
							} else
								System.out
										.println("There're no engaged hosts.");
						} else if (opType.getSelectedItem().equals(
								"outg(TupleType, ITuple[], int)")) {
							
							if (share.getState()) {
								FlatShareTuple[] tuples = 
									createFlatShareTupleArray(new TupleType(tupleType.getText()), g_tuples);
								results = dts.outg(tuples);
							} else {
								FlatSignalTuple[] tuples = 
									createFlatSignalTupleArray(new TupleType(tupleType.getText()), g_tuples);
								results = dts.outg(tuples);
							}
							outPut.append(index + ": " + "outg: " + g_tuples
									+ "\n");
							++index;
							for (int ix = 0; ix < results.length; ++ix) {
								result = results[ix];
								outPut.append(result.toFlatTuple().toString());
								outPut.append("\n");
							}
							outPut.append("\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"outgShareTuple(TupleType, ITuple[])")) {
							FlatSignalTuple[] tuples = new FlatSignalTuple[g_tuples.size()];
							TupleType tt = new TupleType(tupleType.getText());
							for (int ix = 0; ix < g_tuples.size(); ++ix) {
								tuples[ix] = new FlatSignalTuple(tt, g_tuples.elementAt(ix));
							}
							results = dts.outg(tuples);
							outPut.append(index + ": " + "outgShareTuple: "
									+ g_tuples + "\n");
							++index;
							for (int ix = 0; ix < results.length; ++ix) {
								result = results[ix];
								outPut.append(result.toFlatTuple().toString());
								outPut.append("\n");
							}
							outPut.append("\n");
							outPut.selectAll();
						} else if (opType.getSelectedItem().equals(
								"outgBroad(TupleType, ITuple[], int)")) {

							servers = dts.getAllEngagedHosts();
							if (servers.length > 0) {
								g_results = new DracoTuple[servers.length][];
								
								if (share.getState()) {
									FlatShareTuple[] tuples = 
										createFlatShareTupleArray(new TupleType(tupleType.getText()), g_tuples);
									g_results = dts.outgBroad(tuples);
								} else {
									FlatSignalTuple[] tuples = 
										createFlatSignalTupleArray(new TupleType(tupleType.getText()), g_tuples);
									g_results = dts.outgBroad(tuples);
								}
								
								outPut.append(index + ": " + "outgBroad: "
										+ g_tuples + "\n");
								++index;
								for (int ix = 0; ix < g_results.length; ++ix) {
									for (int j = 0; j < g_results[ix].length; ++j) {
										result = g_results[ix][j];
										outPut.append(result.toFlatTuple()
												.toString());
										outPut.append("\n");
									}
								}
								outPut.append("\n");
								outPut.selectAll();
							} else
								System.out
										.println("There're no engaged hosts.");
						} else if (opType
								.getSelectedItem()
								.equals(
										"outgSignalTuple(TupleType, Location, ITuple[])")) {
							if (hosts.getItemCount() > 0) {
								servers = new LimeServerID[arrayServers.size()];
								servers = arrayServers.toArray(servers);
								int i = hosts.getSelectedIndex();
								FlatSignalTuple[] tuples = 
									createFlatSignalTupleArray(new TupleType(
										tupleType.getText()), 
										new Location(servers[i]), g_tuples);
								
								results = dts.outg(tuples);
								outPut
										.append(index + ": "
												+ "outgSignalTuple: "
												+ g_tuples + "\n");
								++index;
								for (int ix = 0; ix < results.length; ++ix) {
									result = results[ix];
									outPut.append(result.toFlatTuple()
											.toString());
									outPut.append("\n");
								}
								outPut.append("\n");
								outPut.selectAll();
							} else
								System.out
										.println("There're no engaged hosts.");
						} else if (opType.getSelectedItem().equals(
								"outg(TupleType, Location, ITuple[], int)")) {
							if (hosts.getItemCount() > 0) {
								servers = new LimeServerID[arrayServers.size()];
								servers = arrayServers.toArray(servers);
								int inx = hosts.getSelectedIndex();

								if (share.getState()) {
									FlatShareTuple[] tuples = createFlatShareTupleArray(
											new TupleType(tupleType.getText()),
											new Location(servers[inx]),
											g_tuples);
									results = dts.outg(tuples);
								} else {
									FlatSignalTuple[] tuples = createFlatSignalTupleArray(
											new TupleType(tupleType.getText()),
											new Location(servers[inx]),
											g_tuples);
									results = dts.outg(tuples);
								}
								outPut.append(index + ": " + "outg: "
										+ g_tuples + "\n");
								++index;
								for (int ix = 0; ix < results.length; ++ix) {
									result = results[ix];
									outPut.append(result.toFlatTuple()
											.toString());
									outPut.append("\n");
								}
								outPut.append("\n");
								outPut.selectAll();
							} else
								System.out
										.println("There're no engaged hosts.");
						} else if (opType
								.getSelectedItem()
								.equals(
										"outgToMulti(TupleType, LimeServerID[], ITuple[], int)")) {
							if (hosts.getItemCount() > 0) {
								if (hosts.getSelectedIndexes().length > 0) {
									servers = new LimeServerID[arrayServers
											.size()];
									servers = arrayServers.toArray(servers);

									g_results = new DracoTuple[servers.length][];
									
									int selectedIndex[] = hosts
											.getSelectedIndexes();
									selectedServers = new LimeServerID[selectedIndex.length];
									for (int ix = 0; ix < selectedIndex.length; ++ix) {
										selectedServers[ix] = servers[selectedIndex[ix]];
									}
									if (share.getState()) {
										FlatShareTuple[] tuples = createFlatShareTupleArray(
												new TupleType(tupleType.getText()),
												g_tuples);
										
										g_results = dts.outgToMulti(tuples,
												Util.toLocationArray(selectedServers));
									} else {
										FlatSignalTuple[] tuples = createFlatSignalTupleArray(
												new TupleType(tupleType.getText()),
												g_tuples);
										
										g_results = dts.outgToMulti(tuples,
												Util.toLocationArray(selectedServers));
									}
									outPut
											.append(index + ": "
													+ "outgToMulti: "
													+ g_tuples + "\n");
									++index;
									for (int ix = 0; ix < g_results.length; ++ix) {
										for (int j = 0; j < g_results[ix].length; ++j) {
											result = g_results[ix][j];
											outPut.append(result.toFlatTuple()
													.toString());
											outPut.append("\n");
										}
									}
									outPut.append("\n");
									outPut.selectAll();
								} else
									System.out
											.println("There're no selected hosts.");
							} else
								System.out
										.println("There're no engaged hosts.");
						}

					}

					g_tuples.removeAllElements();
					tupleTypes.removeAllElements();
					tuples.setText("");
				}
			});
		}
		return jButton8;
	}

	/**
	 * This method initializes tuples2
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getTuples2() {
		if (tuples == null) {
			tuples = new JTextArea();
			tuples.setLineWrap(true);
			tuples.setEditable(false);
		}
		return tuples;
	}

	/**
	 * This method initializes jScrollPane1
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(new Rectangle(356, 31, 250, 129));

			jScrollPane1
					.setHorizontalScrollBarPolicy(jScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jScrollPane1.setViewportView(getTuples2());
			jScrollPane1
					.setVerticalScrollBarPolicy(jScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes hosts
	 * 
	 * @return java.awt.List
	 */
	private List getHosts() {
		if (hosts == null) {
			hosts = new List();
			hosts.setBounds(new Rectangle(20, 24, 233, 73));
			hosts.setMultipleMode(false);
		}
		return hosts;
	}

	/**
	 * This method initializes dataType
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getDataType() {
		if (dataType == null) {
			dataType = new Choice();
			dataType.setBounds(new Rectangle(46, 43, 74, 21));
			dataType.add("String");
			dataType.add("Integer");
			dataType.add("Double");
			dataType.add("Boolean");
			dataType.select("String");
		}
		return dataType;
	}

	/**
	 * This method initializes dataType2
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getDataType2() {
		if (dataType2 == null) {
			dataType2 = new Choice();
			dataType2.setBounds(new Rectangle(2, 32, 78, 21));
			dataType2.add("String");
			dataType2.add("Integer");
			dataType2.add("Double");
			dataType2.add("Boolean");

			dataType2.select("String");
		}
		return dataType2;
	}

	/**
	 * This method initializes choice
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getChoice() {
		if (choice == null) {
			choice = new Choice();
			choice.setBounds(new Rectangle(88, 83, 80, 21));

			choice.add("_*_");
			choice.add("String");
			choice.add("Integer");
			choice.add("Double");
			choice.add("Boolean");
			choice.select("_*_");

			choice.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()");

					if (choice.getSelectedItem().equals("String")) {
						tuple.add(new Field().setType(String.class));
						tupleContent2.setText(tuple.toString());
					} else if (choice.getSelectedItem().equals("Integer")) {
						tuple.add(new Field().setType(Integer.class));
						tupleContent2.setText(tuple.toString());
					} else if (choice.getSelectedItem().equals("Double")) {
						tuple.add(new Field().setType(Double.class));
						tupleContent2.setText(tuple.toString());
					} else if (choice.getSelectedItem().equals("Boolean")) {
						tuple.add(new Field().setType(Boolean.class));
						tupleContent2.setText(tuple.toString());
					} else if (choice.getSelectedItem().equals("_*_")) {
						tuple.add(new Field().setValue("_*_"));
						tupleContent2.setText(tuple.toString());
					}
				}
			});
		}
		return choice;
	}

	/**
	 * This method initializes main_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getMain_panel() {
		if (main_panel == null) {
			localHost = new JLabel();
			localHost.setFont(new Font("Tahoma", Font.PLAIN, 20));
			localHost.setBounds(new Rectangle(8, 6, 357, 27));
			localHost.setText("");
			main_panel = new JPanel();
			main_panel.setLayout(null);
			main_panel.setBounds(new Rectangle(13, 31, 905, 637));
			main_panel.setBackground(new Color(255, 254, 255));
			main_panel.add(startInfo, null);
			main_panel.add(jLabel, null);
			main_panel.add(getEngagedHosts_panel(), null);
			main_panel.add(getRead_panel(), null);
			main_panel.add(getOut_panel(), null);
			main_panel.add(getChange_panel(), null);
			main_panel.add(getOutg_panel(), null);
			main_panel.add(jLabel22, null);
			main_panel.add(getJScrollPane(), null);
			main_panel.setVisible(true);
			main_panel.add(getOpType(), null);
			main_panel.add(localHost, null);
			main_panel.add(getMonitor(), null);
			main_panel.add(getJButton1(), null);
			main_panel.add(getTupleType_panel(), null);
			main_panel.add(getShare_signal_panel(), null);
			main_panel.add(getAddress_panel(), null);
			main_panel.add(getJButton11(), null);
			main_panel.add(getJButton12(), null);
			main_panel.add(getJButton13(), null);
			main_panel.add(getClearReplica_panel(), null);
			main_panel.add(getRPTuple_panel(), null);
		}
		return main_panel;
	}

	/**
	 * This method initializes opType
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getOpType() {
		if (opType == null) {
			opType = new Choice();
			opType.setBounds(new Rectangle(49, 56, 324, 21));
			opType.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()");

					/*
					 * Display different panels according to the chosen
					 * operation type.
					 */
					if (opType.getSelectedItem().equals(
							"out(TupleType, ITuple, int)")
							|| opType.getSelectedItem().equals(
									"outShareTuple(TupleType, ITuple)")
							|| opType.getSelectedItem().equals(
									"outBroad(TupleType, ITuple, int)")) {
						tuple = new Tuple();
						tupleContent.setText("<null>");

						addReplica.setVisible(false);
						removeReplica.setVisible(false);
						pushReplica.setVisible(false);
						matchAllTupleType.setVisible(false);
						toReplicateTuples.removeAllElements();
						toRemoveTuplesVID.removeAllElements();
						toPushTuples.removeAllElements();

						engagedHosts_panel.setVisible(false);
						read_panel.setVisible(false);
						change_panel.setVisible(false);
						outg_panel.setVisible(false);
						checkbox.setVisible(false);
						address_panel.setVisible(false);
						cbg.setSelectedCheckbox(share);
						out_panel.setVisible(true);
						share_signal_panel.setVisible(true);
						if (opType.getSelectedItem().equals(
								"outShareTuple(TupleType, ITuple)"))
							share_signal_panel.setVisible(false);
					} else if (opType.getSelectedItem().equals(
							"out(DracoTuple)")
							|| opType.getSelectedItem().equals(
									"out(TupleType, Location, ITuple, int)")
							|| opType
									.getSelectedItem()
									.equals(
											"outToMulti(TupleType, LimeServerID[], ITuple, int)")
							|| opType
									.getSelectedItem()
									.equals(
											"outSignalTuple(TupleType, Location, ITuple)")) {
						tuple = new Tuple();
						tupleContent.setText("<null>");

						arrayServers.clear();
						engagedHosts_panel.setVisible(true);
						LimeServerID[] tempServers = dts.getAllEngagedHosts();
						hosts.removeAll();
						for (int ix = 0; ix < tempServers.length; ++ix) {
							arrayServers.add(tempServers[ix]);
							hosts.add(tempServers[ix].toString());
						}
						if (hosts.getItemCount() > 0)
							hosts.select(0);

						if (opType
								.getSelectedItem()
								.equals(
										"outToMulti(TupleType, LimeServerID[], ITuple, int)"))
							hosts.setMultipleMode(true);
						else
							hosts.setMultipleMode(false);

						addReplica.setVisible(false);
						removeReplica.setVisible(false);
						pushReplica.setVisible(false);
						matchAllTupleType.setVisible(false);
						toReplicateTuples.removeAllElements();
						toRemoveTuplesVID.removeAllElements();
						toPushTuples.removeAllElements();

						read_panel.setVisible(false);
						change_panel.setVisible(false);
						outg_panel.setVisible(false);
						out_panel.setVisible(true);
						share_signal_panel.setVisible(true);
						address_panel.setVisible(true);
						checkbox.setVisible(false);
						cbg.setSelectedCheckbox(share);
						if (opType.getSelectedItem().equals(
								"outSignalTuple(TupleType, Location, ITuple)"))
							share_signal_panel.setVisible(false);
					} else if (opType.getSelectedItem().equals(
							"outg(TupleType, ITuple[], int)")
							|| opType.getSelectedItem().equals(
									"outgShareTuple(TupleType, ITuple[])")
							|| opType.getSelectedItem().equals(
									"outgBroad(TupleType, ITuple[], int)")) {
						g_tuples.removeAllElements();
						tupleTypes.removeAllElements();
						tuples.setText("");
						tuple = new Tuple();
						tupleContent3.setText("<null>");
						errorInfo.setText("");

						addReplica.setVisible(false);
						removeReplica.setVisible(false);
						pushReplica.setVisible(false);
						matchAllTupleType.setVisible(false);
						toReplicateTuples.removeAllElements();
						toRemoveTuplesVID.removeAllElements();
						toPushTuples.removeAllElements();

						engagedHosts_panel.setVisible(false);
						read_panel.setVisible(false);
						change_panel.setVisible(false);
						outg_panel.setVisible(true);
						out_panel.setVisible(false);
						share_signal_panel.setVisible(true);
						address_panel.setVisible(false);
						checkbox.setVisible(false);
						cbg.setSelectedCheckbox(share);
						if (opType.getSelectedItem().equals(
								"outgShareTuple(TupleType, ITuple[])"))
							share_signal_panel.setVisible(false);
					} else if (opType.getSelectedItem().equals(
							"outg(DracoTuple[])")
							|| opType
									.getSelectedItem()
									.equals(
											"outgSignalTuple(TupleType, Location, ITuple[])")
							|| opType.getSelectedItem().equals(
									"outg(TupleType, Location, ITuple[], int)")
							|| opType
									.getSelectedItem()
									.equals(
											"outgToMulti(TupleType, LimeServerID[], ITuple[], int)")) {
						g_tuples.removeAllElements();
						tuples.setText("");
						tuple = new Tuple();
						tupleContent3.setText("<null>");
						errorInfo.setText("");

						arrayServers.clear();
						engagedHosts_panel.setVisible(true);
						LimeServerID[] tempServers = dts.getAllEngagedHosts();
						hosts.removeAll();
						for (int ix = 0; ix < tempServers.length; ++ix) {
							arrayServers.add(tempServers[ix]);
							hosts.add(tempServers[ix].toString());
						}
						if (hosts.getItemCount() > 0)
							hosts.select(0);

						if (opType
								.getSelectedItem()
								.equals(
										"outgToMulti(TupleType, LimeServerID[], ITuple[], int)"))
							hosts.setMultipleMode(true);
						else
							hosts.setMultipleMode(false);

						addReplica.setVisible(false);
						removeReplica.setVisible(false);
						pushReplica.setVisible(false);
						matchAllTupleType.setVisible(false);
						toReplicateTuples.removeAllElements();
						toRemoveTuplesVID.removeAllElements();
						toPushTuples.removeAllElements();

						read_panel.setVisible(false);
						change_panel.setVisible(false);
						outg_panel.setVisible(true);
						out_panel.setVisible(false);
						share_signal_panel.setVisible(true);
						address_panel.setVisible(true);
						checkbox.setVisible(false);
						cbg.setSelectedCheckbox(share);
						if (opType
								.getSelectedItem()
								.equals(
										"outgSignalTuple(TupleType, Location, ITuple[])"))
							share_signal_panel.setVisible(false);
					} else if (opType.getSelectedItem().equals(
							"change(ShareTuple, ITuple)")) {
						tuple = new Tuple();
						tuple2 = new Tuple();
						tupleContent4.setText("<null>");
						tupleContent5.setText("<null>");

						addReplica.setVisible(false);
						removeReplica.setVisible(false);
						pushReplica.setVisible(false);
						matchAllTupleType.setVisible(true);
						toReplicateTuples.removeAllElements();
						toRemoveTuplesVID.removeAllElements();
						toPushTuples.removeAllElements();

						engagedHosts_panel.setVisible(false);
						out_panel.setVisible(false);
						read_panel.setVisible(false);
						outg_panel.setVisible(false);
						change_panel.setVisible(true);
						share_signal_panel.setVisible(false);
						address_panel.setVisible(false);
						checkbox.setVisible(false);
						cbg.setSelectedCheckbox(share);
					} else if (opType.getSelectedItem()
							.equals("rd(DracoTuple)")
							|| opType.getSelectedItem().equals(
									"rdp(DracoTuple)")
							|| opType.getSelectedItem().equals(
									"rdg(DracoTuple)")
							|| opType.getSelectedItem()
									.equals("in(DracoTuple)")
							|| opType.getSelectedItem().equals(
									"inp(DracoTuple)")
							|| opType.getSelectedItem().equals(
									"ing(DracoTuple)")) {
						tuple = new Tuple();
						tupleContent2.setText("<null>");

						addReplica.setVisible(false);
						removeReplica.setVisible(false);
						pushReplica.setVisible(false);
						matchAllTupleType.setVisible(true);
						toReplicateTuples.removeAllElements();
						toRemoveTuplesVID.removeAllElements();
						toPushTuples.removeAllElements();

						engagedHosts_panel.setVisible(false);
						out_panel.setVisible(false);
						outg_panel.setVisible(false);
						change_panel.setVisible(false);
						address_panel.setVisible(false);
						read_panel.setVisible(true);

						if (opType.getSelectedItem().equals("in(DracoTuple)")
								|| opType.getSelectedItem().equals(
										"inp(DracoTuple)")
								|| opType.getSelectedItem().equals(
										"ing(DracoTuple)")) {
							both.setVisible(false);
							master.setVisible(false);
							replica.setVisible(false);
						} else {
							both.setVisible(true);
							master.setVisible(true);
							replica.setVisible(true);
						}
						share_signal_panel.setVisible(true);
						checkbox.setVisible(true);
					}
				}
			});

			/*
			 * Add operations to choose.
			 */

			// rd
			opType.add("rd(DracoTuple)");
			opType.add("rdp(DracoTuple)");
			opType.add("rdg(DracoTuple)");

			// in
			opType.add("in(DracoTuple)");
			opType.add("inp(DracoTuple)");
			opType.add("ing(DracoTuple)");

			// change
			opType.add("change(ShareTuple, ITuple)");

			// out
			opType.add("out(DracoTuple)");
			opType.add("outg(DracoTuple[])");

			opType.add("out(TupleType, ITuple, int)");
			opType.add("outg(TupleType, ITuple[], int)");

			opType.add("outSignalTuple(TupleType, Location, ITuple)");
			opType.add("outgSignalTuple(TupleType, Location, ITuple[])");

			opType.add("outShareTuple(TupleType, ITuple)");
			opType.add("outgShareTuple(TupleType, ITuple[])");

			opType.add("out(TupleType, Location, ITuple, int)");
			opType.add("outg(TupleType, Location, ITuple[], int)");

			opType.add("outToMulti(TupleType, LimeServerID[], ITuple, int)");
			opType.add("outgToMulti(TupleType, LimeServerID[], ITuple[], int)");

			opType.add("outBroad(TupleType, ITuple, int)");
			opType.add("outgBroad(TupleType, ITuple[], int)");

			opType.select("out(TupleType, ITuple, int)");
		}
		return opType;
	}

	/**
	 * This method initializes dataType3
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getDataType3() {
		if (dataType3 == null) {
			dataType3 = new Choice();
			dataType3.setBounds(new Rectangle(29, 15, 77, 20));

			dataType3.add("String");
			dataType3.add("Integer");
			dataType3.add("Double");
			dataType3.add("Boolean");
			dataType3.select("String");
		}
		return dataType3;
	}

	/**
	 * This method initializes dataType4
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getDataType4() {
		if (dataType4 == null) {
			dataType4 = new Choice();
			dataType4.setBounds(new Rectangle(16, 38, 74, 21));

			dataType4.add("String");
			dataType4.add("Integer");
			dataType4.add("Double");
			dataType4.add("Boolean");

			dataType4.select(0);
		}
		return dataType4;
	}

	/**
	 * This method initializes dataType5
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getDataType5() {
		if (dataType5 == null) {
			dataType5 = new Choice();
			dataType5.setBounds(new Rectangle(366, 37, 72, 21));

			dataType5.add("String");
			dataType5.add("Integer");
			dataType5.add("Double");
			dataType5.add("Boolean");
			dataType5.select("String");
		}
		return dataType5;
	}

	/**
	 * This method initializes monitor
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getMonitor() {
		if (monitor == null) {
			monitor = new JCheckBox();
			monitor.setBounds(new Rectangle(261, 438, 121, 21));
			monitor.setText("Turn On Monitor");
			monitor.setSelected(true);
			monitor.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()");

					if (monitor.isSelected()) {
						// Turn on the monitor to keep record of operations
						dts.setMonitorStatus(true);
						System.out.println("Turn on monitor.");
					} else {
						// Turn off the monitor
						dts.setMonitorStatus(false);
						System.out.println("Turn off monitor.");
					}
				}
			});
		}
		return monitor;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(387, 437, 137, 23));
			jButton1.setText("Save Log to File");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					// output the log file and clear the log cache
					// You can use the first parameter of function outputLog
					// to set output path.
					String fullPath = dts.outputLog(null, true);
					System.out.println("Finish writing log file " + fullPath);
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes tupleType_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTupleType_panel() {
		if (tupleType_panel == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(5, 13, 68, 25));
			jLabel3.setText("Tuple Type: ");
			tupleType_panel = new JPanel();
			tupleType_panel.setLayout(null);
			tupleType_panel.setBounds(new Rectangle(17, 127, 357, 71));
			tupleType_panel.setVisible(true);
			tupleType_panel.add(jLabel3, null);
			tupleType_panel.add(getTupleType(), null);
			tupleType_panel.add(getMatchAllTupleType(), null);
		}
		return tupleType_panel;
	}

	/**
	 * This method initializes tupleType
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTupleType() {
		if (tupleType == null) {
			tupleType = new JTextField();
			tupleType.setBounds(new Rectangle(73, 14, 275, 25));
			tupleType.setText("default type");
		}
		return tupleType;
	}

	/**
	 * This method initializes share_signal_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getShare_signal_panel() {
		if (share_signal_panel == null) {
			share_signal_panel = new JPanel();
			share_signal_panel.setLayout(null);
			share_signal_panel.setBounds(new Rectangle(17, 85, 357, 42));
			share_signal_panel.add(getShare(), null);
			share_signal_panel.add(getSignal(), null);
			share_signal_panel.setVisible(true);

			share_signal_panel.add(getCheckbox(), null);
			share_signal_panel.add(getJCheckBox(), null);
		}
		return share_signal_panel;
	}

	/**
	 * This method initializes share
	 * 
	 * @return java.awt.Checkbox
	 */
	private Checkbox getShare() {
		if (share == null) {
			share = new Checkbox("SHARE", cbg, true);
			share.setBounds(new Rectangle(15, 7, 59, 18));
		}
		return share;
	}

	/**
	 * This method initializes signal
	 * 
	 * @return java.awt.Checkbox
	 */
	private Checkbox getSignal() {
		if (signal == null) {
			signal = new Checkbox("SIGNAL", cbg, true);
			cbg.setSelectedCheckbox(share);
			signal.setBounds(new Rectangle(85, 8, 64, 15));
		}
		return signal;
	}

	/**
	 * This method initializes choice1
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getChoice1() {
		if (choice1 == null) {
			choice1 = new Choice();
			choice1.setBounds(new Rectangle(83, 136, 80, 19));
			choice1.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()");

					if (choice1.getSelectedItem().equals("String")) {
						tuple.add(new Field().setType(String.class));
						tupleContent4.setText(tuple.toString());
					} else if (choice1.getSelectedItem().equals("Integer")) {
						tuple.add(new Field().setType(Integer.class));
						tupleContent4.setText(tuple.toString());
					} else if (choice1.getSelectedItem().equals("Double")) {
						tuple.add(new Field().setType(Double.class));
						tupleContent4.setText(tuple.toString());
					} else if (choice1.getSelectedItem().equals("Boolean")) {
						tuple.add(new Field().setType(Boolean.class));
						tupleContent4.setText(tuple.toString());
					} else if (choice1.getSelectedItem().equals("_*_")) {
						tuple.add(new Field().setValue("_*_"));
						tupleContent4.setText(tuple.toString());
					}
				}
			});

			choice1.add("_*_");
			choice1.add("String");
			choice1.add("Integer");
			choice1.add("Double");
			choice1.add("Boolean");
			choice1.select("_*_");
		}
		return choice1;
	}

	/**
	 * This method initializes ranTupleSize
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getRanTupleSize() {
		if (ranTupleSize == null) {
			ranTupleSize = new JTextField();
			ranTupleSize.setBounds(new Rectangle(192, 156, 55, 21));
			ranTupleSize.setText("1024");
		}
		return ranTupleSize;
	}

	/**
	 * This method initializes addReplica
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAddReplica() {
		if (addReplica == null) {
			addReplica = new JButton();
			addReplica.setText("Add Replica");
			addReplica.setBounds(new Rectangle(362, 121, 105, 25));
			addReplica.setVisible(false);
			addReplica.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					int len = toReplicateTuples.size();
					DracoTuple[] sts = new DracoTuple[len];

					for (int ix = 0; ix < len; ++ix)
						sts[ix] = toReplicateTuples.elementAt(ix);
					CommandReactionListener.PostingData entriesToSend = new CommandReactionListener.PostingData();

					outPut.append(index + ": " + "add replica:\n");
					++index;

					for (int i = 0; i < len; ++i)
						entriesToSend.putData(sts[i].getCurr(), sts[i]);
					Location[] wheretos = entriesToSend.getAllDest();
					for (int i = 0; i < wheretos.length; ++i) {
						Object[] obj_sts = entriesToSend.getData(wheretos[i]);
						if (obj_sts != null) {
							ShareTuple[] st_sts = new ShareTuple[obj_sts.length];
							AddReplicaRequestByEntryCommand.ReplicaRequestEntry[] entries = new AddReplicaRequestByEntryCommand.ReplicaRequestEntry[st_sts.length];

							for (int j = 0; j < st_sts.length; ++j) {
								st_sts[j] = (ShareTuple) obj_sts[j];
								entries[j] = new AddReplicaRequestByEntryCommand.ReplicaRequestEntry(
										((ShareTuple) st_sts[j]).getVID(),
										((ShareTuple) st_sts[j]).getVer());

								ShareTuple template = ShareTuple
										.createVIDTemplate(entries[j].vid);
								template.setCurr(Location.HERE);
								if (dts.rdp(template) == null) {
									ShareTuple replica = ((ShareTuple) st_sts[j]).replicate(Location.HERE);
									outPut.append(dts.out(replica).toFlatTuple().toString());
									outPut.append("\n");
								} else {
									ShareTuple replica = ((ShareTuple) st_sts[j]).replicate(Location.HERE);
									outPut.append("Replica "
											+ replica.toFlatTuple().toString()
											+ " already exist.\n");
								}
							}
							dts.addReplica(st_sts[0].getCurr(), entries);
						}
					}

					outPut.append("\n");
					outPut.selectAll();

					toReplicateTuples.removeAllElements();
					addReplica.setVisible(false);
				}
			});
		}
		return addReplica;
	}

	/**
	 * This method initializes both
	 * 
	 * @return java.awt.Checkbox
	 */
	private Checkbox getBoth() {
		if (both == null) {
			both = new Checkbox("Both", cbg2, true);
			cbg2.setSelectedCheckbox(both);
			both.setBounds(new Rectangle(416, 86, 47, 21));
			both.setLabel("Both");
			both.setVisible(false);
		}
		return both;
	}

	/**
	 * This method initializes master
	 * 
	 * @return java.awt.Checkbox
	 */
	private Checkbox getMaster() {
		if (master == null) {
			master = new Checkbox("Master", cbg2, true);
			cbg2.setSelectedCheckbox(both);
			master.setBounds(new Rectangle(292, 90, 58, 13));
			master.setLabel("Master");
			master.setVisible(false);
		}
		return master;
	}

	/**
	 * This method initializes replica
	 * 
	 * @return java.awt.Checkbox
	 */
	private Checkbox getReplica() {
		if (replica == null) {
			replica = new Checkbox("Replica", cbg2, true);
			cbg2.setSelectedCheckbox(both);
			replica.setBounds(new Rectangle(351, 89, 63, 15));
			replica.setLabel("Replica");
			replica.setVisible(false);
		}
		return replica;
	}

	/**
	 * This method initializes checkbox
	 * 
	 * @return java.awt.Checkbox
	 */
	private Checkbox getCheckbox() {
		if (checkbox == null) {
			checkbox = new Checkbox("All", cbg, true);
			cbg.setSelectedCheckbox(share);
			checkbox.setBounds(new Rectangle(160, 5, 152, 22));
			checkbox.setLabel("MATCH_ALL_TEMPLATE");
			checkbox.setVisible(false);
		}
		return checkbox;
	}

	/**
	 * This method initializes removeReplica
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRemoveReplica() {
		if (removeReplica == null) {
			removeReplica = new JButton();
			removeReplica.setBounds(new Rectangle(337, 148, 130, 24));
			removeReplica.setText("Remove Replica");
			removeReplica.setVisible(false);
			removeReplica
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							System.out.println("actionPerformed()");

							int len = toRemoveTuplesVID.size();
							TupleID[] tempTuplesVID = new TupleID[len];
							for (int ix = 0; ix < len; ++ix)
								tempTuplesVID[ix] = toRemoveTuplesVID
										.elementAt(ix);
							DracoTuple[] removedReplicas = dts
									.removeReplica(tempTuplesVID);
							outPut.append(index + ": " + "remove replica:\n");
							++index;
							int size = removedReplicas.length;
							for (int ix = 0; ix < size; ++ix) {
								outPut.append(removedReplicas[ix].toFlatTuple()
										.toString());
								outPut.append("\n");
							}
							outPut.append("\n");
							outPut.selectAll();

							toRemoveTuplesVID.removeAllElements();
							removeReplica.setVisible(false);
						}
					});
		}
		return removeReplica;
	}

	/**
	 * This method initializes address_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAddress_panel() {
		if (address_panel == null) {
			jLabel15 = new JLabel();
			jLabel15.setBounds(new Rectangle(56, 62, 32, 20));
			jLabel15.setText("Port:");
			jLabel14 = new JLabel();
			jLabel14.setBounds(new Rectangle(3, 33, 86, 23));
			jLabel14.setText("Host Address:");
			jLabel11 = new JLabel();
			jLabel11.setBounds(new Rectangle(18, 5, 71, 23));
			jLabel11.setText("Host Name:");
			address_panel = new JPanel();
			address_panel.setLayout(null);
			address_panel.setBounds(new Rectangle(644, 85, 242, 113));
			address_panel.add(jLabel11, null);
			address_panel.add(jLabel14, null);
			address_panel.add(jLabel15, null);
			address_panel.add(getHostName(), null);
			address_panel.add(getIp0(), null);
			address_panel.add(getIp1(), null);
			address_panel.add(getIp2(), null);
			address_panel.add(getIp3(), null);
			address_panel.add(getPortNum(), null);
			address_panel.add(getJButton10(), null);
			address_panel.setVisible(false);
		}
		return address_panel;
	}

	/**
	 * This method initializes hostName
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getHostName() {
		if (hostName == null) {
			hostName = new JTextField();
			hostName.setBounds(new Rectangle(90, 6, 144, 22));
		}
		return hostName;
	}

	/**
	 * This method initializes ip0
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getIp0() {
		if (ip0 == null) {
			ip0 = new JTextField();
			ip0.setBounds(new Rectangle(91, 34, 29, 22));
			ip0.setText("59");
		}
		return ip0;
	}

	/**
	 * This method initializes ip1
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getIp1() {
		if (ip1 == null) {
			ip1 = new JTextField();
			ip1.setBounds(new Rectangle(121, 34, 29, 22));
			ip1.setText("66");
		}
		return ip1;
	}

	/**
	 * This method initializes ip2
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getIp2() {
		if (ip2 == null) {
			ip2 = new JTextField();
			ip2.setBounds(new Rectangle(151, 34, 29, 22));
			ip2.setText("153");
		}
		return ip2;
	}

	/**
	 * This method initializes ip3
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getIp3() {
		if (ip3 == null) {
			ip3 = new JTextField();
			ip3.setBounds(new Rectangle(181, 34, 29, 22));
			ip3.setText("229");
		}
		return ip3;
	}

	/**
	 * This method initializes portNum
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPortNum() {
		if (portNum == null) {
			portNum = new JTextField();
			portNum.setBounds(new Rectangle(90, 63, 50, 20));
			portNum.setText("1973");
		}
		return portNum;
	}

	/**
	 * This method initializes jButton10
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton10() {
		if (jButton10 == null) {
			jButton10 = new JButton();
			jButton10.setBounds(new Rectangle(166, 76, 62, 27));
			jButton10.setText("Add");
			jButton10.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					// Get host name.
					String hostname = hostName.getText();

					// Get IP address.
					byte[] ipAddress = new byte[4];
					ipAddress[0] = (byte) Integer.parseInt(ip0.getText());
					ipAddress[1] = (byte) Integer.parseInt(ip1.getText());
					ipAddress[2] = (byte) Integer.parseInt(ip2.getText());
					ipAddress[3] = (byte) Integer.parseInt(ip3.getText());

					// Get port.
					int port = (int) Integer.parseInt(portNum.getText());

					InetAddress inet = null;
					if (!hostname.equals("")) {
						try {
							inet = InetAddress.getByName(hostname);
							addedServers
									.addElement(new LimeServerID(inet, port));
						} catch (Exception ex) {
							System.out.println("Invalid Host Name.");
							ex.printStackTrace();
						}
					}

					try {
						inet = InetAddress.getByAddress(ipAddress);
						addedServers.addElement(new LimeServerID(inet, port));
					} catch (Exception ex2) {
						System.out.println("Invalid Host Address.");
						ex2.printStackTrace();
					}

					int len = addedServers.size();

					for (int ix = 0; ix < len; ++ix) {
						arrayServers.add(addedServers.elementAt(ix));
						hosts.add(addedServers.elementAt(ix).toString());
					}
					hosts.select(0);

					addedServers.removeAllElements();
				}
			});
		}
		return jButton10;
	}

	/**
	 * This method initializes jButton11
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton11() {
		if (jButton11 == null) {
			jButton11 = new JButton();
			jButton11.setBounds(new Rectangle(753, 4, 150, 24));
			jButton11.setFont(new Font("High Tower Text", Font.BOLD, 14));
			jButton11.setForeground(new Color(255, 51, 51));
			jButton11.setText("Kill VisualAgent");
			jButton11.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					System.out.println("\nExit VisualAgent: "
							+ Location.HERE.toString());
					dispose();
				}
			});
		}
		return jButton11;
	}

	/**
	 * This method initializes jButton9
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton9() {
		if (jButton9 == null) {
			jButton9 = new JButton();
			jButton9.setBounds(new Rectangle(34, 153, 157, 25));
			jButton9.setFont(new Font("High Tower Text", Font.PLAIN, 13));
			jButton9.setForeground(new Color(255, 102, 0));
			jButton9.setText("Out a Tuple Randomly");
			jButton9.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					// out a tuple randomly.
					Random random = new Random();
					double tupleSize = 0;
					double totalSize = 0;
					if (ranTupleSize.getText().equals(""))
						totalSize = 0;
					else
						totalSize = 1024 * Double.parseDouble(ranTupleSize
								.getText());
					do {
						int choice = random.nextInt(6);
						int what = random.nextInt(30) + 1;
						switch (choice) {
						case 0:
							String str = getRanString(what);
							tuple.add(new Field().setValue(str));
							tupleSize += str.length() * 2;
							break;
						case 1:
							tuple.add(new Field()
									.setValue(random.nextBoolean()));
							tupleSize += 1 / 8;
							break;
						case 2:
							tuple.add(new Field().setValue(random.nextInt()));
							tupleSize += 4;
							break;
						case 3:
							tuple
									.add(new Field().setValue(random
											.nextDouble()));
							tupleSize += 8;
							break;
						case 4:
							tuple.add(new Field().setValue(random
									.nextGaussian()));
							tupleSize += 8;
							break;
						case 5:
							tuple.add(new Field().setValue(random
									.nextInt(80000)));
							tupleSize += 4;
							break;
						}

					} while (tupleSize < totalSize);

					if (opType.getSelectedItem().equals("out(DracoTuple)")) {
						if (hosts.getItemCount() > 0) {
							servers = new LimeServerID[arrayServers.size()];
							servers = arrayServers.toArray(servers);

							int inx = hosts.getSelectedIndex();
							if (share.getState())
								result = dts.out(new FlatShareTuple(
										new TupleType(tupleType.getText()),
										new Location(servers[inx]), tuple));
							else
								result = dts.out(new FlatSignalTuple(
										new TupleType(tupleType.getText()),
										new Location(servers[inx]), tuple));
						} else
							System.out.println("There're no engaged hosts.");
					} else if (opType.getSelectedItem().equals(
							"out(TupleType, ITuple, int)"))
						if (share.getState())
							result = dts.out(new FlatShareTuple(
									new TupleType(tupleType.getText()), tuple));
						else
							result = dts.out(
									new FlatSignalTuple(
											new TupleType(tupleType.getText()), tuple));
					else if (opType.getSelectedItem().equals(
							"outShareTuple(TupleType, ITuple)"))
						result = dts.out(new FlatShareTuple(new TupleType(tupleType
								.getText()), tuple));
					else if (opType.getSelectedItem().equals(
							"outBroad(TupleType, ITuple, int)")) {
						servers = dts.getAllEngagedHosts();
						if (servers.length > 0) {
							if (share.getState())
								dts.outBroad(
										new FlatShareTuple(new TupleType(tupleType
										.getText()), tuple));
							else
								dts.outBroad(
										new FlatSignalTuple(new TupleType(tupleType
										.getText()), tuple));
						} else
							System.out.println("There're no engaged hosts.");
					} else if (opType.getSelectedItem().equals(
							"out(TupleType, Location, ITuple, int)")) {
						if (hosts.getItemCount() > 0) {
							servers = new LimeServerID[arrayServers.size()];
							servers = arrayServers.toArray(servers);

							int inx = hosts.getSelectedIndex();
							if (share.getState())
								result = dts.out(new FlatShareTuple(
										new TupleType(tupleType.getText()),
										new Location(servers[inx]), tuple));

							else
								result = dts.out(new FlatSignalTuple(
										new TupleType(tupleType.getText()),
										new Location(servers[inx]), tuple));
						} else
							System.out.println("There're no engaged hosts.");
					} else if (opType
							.getSelectedItem()
							.equals(
									"outToMulti(TupleType, LimeServerID[], ITuple, int)")) {
						if (hosts.getItemCount() > 0) {
							if (hosts.getSelectedIndexes().length > 0) {
								servers = new LimeServerID[arrayServers.size()];
								servers = arrayServers.toArray(servers);

								int selectedIndex[] = hosts
										.getSelectedIndexes();
								selectedServers = new LimeServerID[selectedIndex.length];
								for (int ix = 0; ix < selectedIndex.length; ++ix) {
									selectedServers[ix] = servers[selectedIndex[ix]];
								}
								if (share.getState())
									dts.outToMulti(new FlatShareTuple(
											new TupleType(tupleType.getText()), tuple),
											Util.toLocationArray(selectedServers));
								else
									dts.outToMulti(new FlatSignalTuple(
											new TupleType(tupleType.getText()), tuple),
											Util.toLocationArray(selectedServers));
							} else
								System.out
										.println("There're no selected hosts.");
						} else
							System.out.println("There're no engaged hosts.");
					} else if (opType.getSelectedItem().equals(
							"outSignalTuple(TupleType, Location, ITuple)")) {
						if (hosts.getItemCount() > 0) {
							servers = new LimeServerID[arrayServers.size()];
							servers = arrayServers.toArray(servers);

							int inx = hosts.getSelectedIndex();
							result = dts.out(new FlatSignalTuple(
										new TupleType(tupleType.getText()),
										new Location(servers[inx]),
									    tuple));
						} else
							System.out.println("There're no engaged hosts.");
					}

					tuple = new Tuple();
				}

				// return a random string.
				private String getRanString(int intLength) {
					String retStr = "";
					String strTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
					int len = strTable.length();
					int count = 0;
					do {
						count = 0;

						for (int i = 0; i < intLength; i++) {
							Random random = new Random();
							int r = random.nextInt(len);
							char c = strTable.charAt(r);
							if (c > '0' && c < '9')
								count++;

							retStr += c;
						}
					} while (count > 1);

					return retStr;
				}
			});
		}
		return jButton9;
	}

	/**
	 * This method initializes jCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox.setBounds(new Rectangle(318, 11, 0, 0));
		}
		return jCheckBox;
	}

	/**
	 * This method initializes matchAllTupleType
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getMatchAllTupleType() {
		if (matchAllTupleType == null) {
			matchAllTupleType = new JCheckBox();
			matchAllTupleType.setBounds(new Rectangle(5, 42, 173, 21));
			matchAllTupleType.setText("MATCH_ALL_TUPLETYPE");
			matchAllTupleType.setVisible(false);
		}
		return matchAllTupleType;
	}

	/**
	 * This method initializes jButton12
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton12() {
		if (jButton12 == null) {
			jButton12 = new JButton();
			jButton12.setBounds(new Rectangle(546, 466, 162, 40));
			jButton12.setActionCommand("Show ReplicaRegistrar");
			jButton12.setText("Show ReplicaRegistar");
			jButton12.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					String str = dts.getReplicaRequestRegistrar().toString();
					outPut.append(index + ": show replicaRegistrar:\n");
					++index;
					outPut.append(str);
					outPut.append("\n\n");
					outPut.selectAll();

					clearReplica_panel.setVisible(true);

					vids = dts.getReplicaRequestRegistrar().getVIDs();
					masterTuplesRe.removeAll();
					replicaLocation.removeAll();
					if (vids != null) {
						int vidLen = vids.length;
						replicaLocations = new Location[vidLen][];
						for (int ix = 0; ix < vidLen; ++ix) {
							masterTuplesRe.add(vids[ix].toString());
							replicaLocations[ix] = dts.getReplicaRequestRegistrar()
									.getReplicaLocations(vids[ix]);
						}
					}
				}
			});
		}
		return jButton12;
	}

	/**
	 * This method initializes jButton13
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton13() {
		if (jButton13 == null) {
			jButton13 = new JButton();
			jButton13.setBounds(new Rectangle(546, 547, 162, 40));
			jButton13.setText("Show PolicyRegistrar");
			jButton13.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					outPut.append(index + ": show policyRegistrar:\n");
					++index;
					String str = dts.getPolicyRegistrar().toString();
					outPut.append(str);
					outPut.append("\n\n");
					outPut.selectAll();
				}
			});
		}
		return jButton13;
	}

	/**
	 * This method initializes pushReplica
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getPushReplica() {
		if (pushReplica == null) {
			pushReplica = new JButton();
			pushReplica.setBounds(new Rectangle(356, 176, 111, 23));
			pushReplica.setText("Push Replica");
			pushReplica.setVisible(false);
			pushReplica.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					if (hosts.getItemCount() > 0) {
						if (hosts.getSelectedIndexes().length > 0) {

							// Get size of master tuples.
							int toPushLen = toPushTuples.size();

							// Get host addresses to push replica.
							servers = new LimeServerID[arrayServers.size()];
							servers = arrayServers.toArray(servers);
							int selectedIndex[] = hosts.getSelectedIndexes();
							int selectedLen = selectedIndex.length;
							selectedServers = new LimeServerID[selectedLen];
							for (int ix = 0; ix < selectedLen; ++ix) {
								selectedServers[ix] = servers[selectedIndex[ix]];
							}

							// Get replica tuples.
							int totalLen = toPushLen * selectedLen;
							ShareTuple[] sts = new ShareTuple[totalLen];

							// Push replica.
							outPut.append(index + ": " + "push replica:\n");
							++index;
							Location[] targets = new Location[selectedLen];
							for (int ix = 0; ix < selectedLen; ++ix){
								targets[ix] = new Location(selectedServers[ix]);
								for (int jx = 0; jx < toPushLen; ++jx) {
									
									sts[jx * selectedLen + ix] =
										((ShareTuple) toPushTuples.elementAt(jx)).
										replicate(targets[ix]);
									outPut.append(sts[jx * selectedLen + ix]
											.toFlatTuple().toString());
									outPut.append("\n");
								}
							}
							outPut.append("\n");
							outPut.selectAll();

							dts.pushReplica(sts, targets);

							toPushTuples.removeAllElements();
							pushReplica.setVisible(false);
							engagedHosts_panel.setVisible(false);
							address_panel.setVisible(false);
						} else
							System.out.println("There're no selected hosts.");
					} else
						System.out.println("There're no engaged hosts.");
				}
			});
		}
		return pushReplica;
	}

	/**
	 * This method initializes clearReplica_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getClearReplica_panel() {
		if (clearReplica_panel == null) {
			jLabel17 = new JLabel();
			jLabel17.setBounds(new Rectangle(4, 123, 105, 18));
			jLabel17.setText("Replica Location:");
			jLabel18 = new JLabel();
			jLabel18.setBounds(new Rectangle(3, 5, 53, 17));
			jLabel18.setText(" VID:");
			clearReplica_panel = new JPanel();
			clearReplica_panel.setLayout(null);
			clearReplica_panel.setBounds(new Rectangle(719, 200, 177, 255));
			clearReplica_panel.add(jLabel18, null);
			clearReplica_panel.add(getMasterTuplesRe(), null);
			clearReplica_panel.add(jLabel17, null);
			clearReplica_panel.add(getReplicaLocation(), null);
			clearReplica_panel.add(getClearReplica(), null);
			clearReplica_panel.setVisible(false);
		}
		return clearReplica_panel;
	}

	/**
	 * This method initializes masterTuplesRe
	 * 
	 * @return java.awt.List
	 */
	private List getMasterTuplesRe() {
		if (masterTuplesRe == null) {
			masterTuplesRe = new List();
			masterTuplesRe.setBounds(new Rectangle(3, 22, 171, 79));
			masterTuplesRe.setMultipleMode(false);

			masterTuplesRe.addItemListener(new java.awt.event.ItemListener() {

				public void itemStateChanged(ItemEvent ie) {
					int selectedVID = masterTuplesRe.getSelectedIndex();
					int selectedVIDLocationLen = 0;
					if (replicaLocations[selectedVID] != null) {
						selectedVIDLocationLen = replicaLocations[selectedVID].length;
						replicaLocation.removeAll();
						for (int ix = 0; ix < selectedVIDLocationLen; ++ix) {
							replicaLocation
									.add(replicaLocations[selectedVID][ix]
											.toString());
						}
						replicaLocation.select(0);
					}
				}
			});
		}
		return masterTuplesRe;
	}

	/**
	 * This method initializes replicaLocation
	 * 
	 * @return java.awt.List
	 */
	private List getReplicaLocation() {
		if (replicaLocation == null) {
			replicaLocation = new List();
			replicaLocation.setBounds(new Rectangle(4, 141, 171, 63));
			replicaLocation.setMultipleMode(true);
		}
		return replicaLocation;
	}

	/**
	 * This method initializes clearReplica
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getClearReplica() {
		if (clearReplica == null) {
			clearReplica = new JButton();
			clearReplica.setBounds(new Rectangle(8, 218, 112, 30));
			clearReplica.setText("Clear Replica");
			clearReplica.setVisible(true);
			clearReplica.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					int selectedVID = masterTuplesRe.getSelectedIndex();
					int selectedLocation[] = replicaLocation
							.getSelectedIndexes();
					if (selectedVID > -1 && selectedLocation.length > 0) {
						TupleID vid = vids[selectedVID];

						outPut.append(index + ": clear replica of VID: "
								+ vid.toString() + "\n");
						++index;
						for (int ix = 0; ix < selectedLocation.length; ++ix) {
							dts
									.clearReplica(
											new TupleID[] { vid },
											replicaLocations[selectedVID][selectedLocation[ix]]);
							outPut
									.append(replicaLocations[selectedVID][selectedLocation[ix]]
											.toString());
							outPut.append("\n");
						}
						outPut.append("\n");
						outPut.selectAll();

						vids = dts.getReplicaRequestRegistrar().getVIDs();
						masterTuplesRe.removeAll();
						replicaLocation.removeAll();
						if (vids != null) {
							int vidLen = vids.length;
							replicaLocations = new Location[vidLen][];
							for (int ix = 0; ix < vidLen; ++ix) {
								masterTuplesRe.add(vids[ix].toString());
								replicaLocations[ix] = dts
										.getReplicaRequestRegistrar()
										.getReplicaLocations(vids[ix]);
							}
						}

					} else
						System.out
								.println("No VID or Location has been selected.");
				}
			});
		}
		return clearReplica;
	}

	/**
	 * This method initializes RPTuple_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getRPTuple_panel() {
		if (RPTuple_panel == null) {
			jLabel23 = new JLabel();
			jLabel23.setBounds(new Rectangle(5, 70, 108, 25));
			jLabel23.setText("Replication Policy :");
			jLabel20 = new JLabel();
			jLabel20.setBounds(new Rectangle(5, 5, 74, 22));
			jLabel20.setText("Tuple Type:");
			RPTuple_panel = new JPanel();
			RPTuple_panel.setLayout(null);
			RPTuple_panel.setBounds(new Rectangle(719, 468, 173, 157));
			RPTuple_panel.add(jLabel20, null);
			RPTuple_panel.add(getTupleType2(), null);
			RPTuple_panel.add(jLabel23, null);
			RPTuple_panel.add(getReplicationPolicy(), null);
			RPTuple_panel.add(getJButton14(), null);
		}
		return RPTuple_panel;
	}

	/**
	 * This method initializes tupleType2
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTupleType2() {
		if (tupleType2 == null) {
			tupleType2 = new JTextField();
			tupleType2.setBounds(new Rectangle(5, 28, 165, 25));
			tupleType2.setText("target type");
		}
		return tupleType2;
	}

	/**
	 * This method initializes replicationPolicy
	 * 
	 * @return java.awt.Choice
	 */
	private Choice getReplicationPolicy() {
		if (replicationPolicy == null) {
			replicationPolicy = new Choice();
			replicationPolicy.setBounds(new Rectangle(114, 72, 56, 21));
			replicationPolicy.add("FR");
			replicationPolicy.add("SO");
			replicationPolicy.add("RC");
			replicationPolicy.select("RC");
		}
		return replicationPolicy;
	}

	/**
	 * This method initializes jButton14
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton14() {
		if (jButton14 == null) {
			jButton14 = new JButton();
			jButton14.setBounds(new Rectangle(50, 110, 115, 37));
			jButton14.setText("Apply Policy");
			jButton14.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//System.out.println("actionPerformed()");

					RPTuple rpTuple = new RPTuple(new TupleType(tupleType2
							.getText()), ReplicationPolicy.UNKNOWN);

					// Set replication policy.
					if (replicationPolicy.getSelectedItem().equals("FR"))
						rpTuple.setPolicy(ReplicationPolicy.FR);
					else if (replicationPolicy.getSelectedItem().equals("SO"))
						rpTuple.setPolicy(ReplicationPolicy.SO);
					else if (replicationPolicy.getSelectedItem().equals("RC"))
						rpTuple.setPolicy(ReplicationPolicy.RC);

					// broadcast policy.
					dts.out(rpTuple);

					outPut.append(index + ": " + "apply policy "
							+ replicationPolicy.getSelectedItem() + " for "
							+ "'" + "'" + tupleType2.getText() + "'" + "'");
					++index;
					outPut.append("\n\n");
					outPut.selectAll();
				}
			});
		}
		return jButton14;
	}

	/**
	 * @return
	 * @author Xing Jiankuan
	 */
	public FlatShareTuple[] createFlatShareTupleArray(TupleType tt, java.util.List its) {
		FlatShareTuple[] tuples = new FlatShareTuple[its.size()];
		
		for (int ix = 0; ix < its.size(); ++ix) {
			tuples[ix] = new FlatShareTuple(tt, (ITuple) its.get(ix));
		}
		return tuples;
	}
	
	public FlatShareTuple[] createFlatShareTupleArray(TupleType tt, Location dest, java.util.List its) {
		FlatShareTuple[] tuples = new FlatShareTuple[its.size()];
		
		for (int ix = 0; ix < its.size(); ++ix) {
			tuples[ix] = new FlatShareTuple(tt, dest, (ITuple) its.get(ix));
		}
		return tuples;
	}
	
	public FlatSignalTuple[] createFlatSignalTupleArray(TupleType tt, java.util.List its) {
		FlatSignalTuple[] tuples = new FlatSignalTuple[its.size()];
		
		for (int ix = 0; ix < its.size(); ++ix) {
			tuples[ix] = new FlatSignalTuple(tt, (ITuple) its.get(ix));
		}
		return tuples;
	}
	
	public FlatSignalTuple[] createFlatSignalTupleArray(TupleType tt, Location dest, java.util.List its) {
		FlatSignalTuple[] tuples = new FlatSignalTuple[its.size()];
		
		for (int ix = 0; ix < its.size(); ++ix) {
			tuples[ix] = new FlatSignalTuple(tt, dest, (ITuple) its.get(ix));
		}
		return tuples;
	}
}
